dojo.provide("dojot.util.Textarea");
dojo.require("dojot._base");
dojo.require("dojo.string");
dojo.requireLocalization("dojot", "common","zh");
dojo.declare("dojot.util.Textarea",null,{
	//默认区域的宽高
	width:"500px",
	
	height:"200px",
	
	//用户当前输入的文字长度
	currertContentLength:0,
	//用户最大可以输入的文字长度
	maxContentLength:200,
	containerElement:null,
	//用户输入文字提示信息template
	contentLengthSuggestTemplate: "${currertContentLength}/${maxContentLength}",
	
	constructor: function(obj){
		//默认的一些主要参数设置
		dojo.mixin(this,obj);
		//模板方法,交给子类有机会初始化一些信息
		this._init();
		this.createElements(this.id);
		this._styleElements();
		this._bindEvent();
		dojo.subscribe("overLength",dojo.hitch(this,"_dealWithOverLength"));	
	},
	_init:function(){
		//国际化信息
		this._messages = dojo.i18n.getLocalization("dojot", "common", dojo.locale);
		dojo.forEach(["buttonOk","buttonCancel"], function(prop){
			if(!this[prop]){ this[prop] = this._messages[prop]; }
		}, this);
		console.log(this._messages);
	},
	createElements: function(){
		
		this.errorElement = this._createElement("p",null,this.containerElement);
		//创建提示元素,比如评论
		this.titleElement = this._createElement("p",this.title,this.containerElement);
		this.textareaElement = document.createElement("textarea");
		dojo.attr(this.textareaElement,{
			rows:4
		});
		dojo.style(this.textareaElement,{
			width:this.width
		});
		var textareaParentElement = this._createElement("div",null,this.containerElement);
		textareaParentElement.appendChild(this.textareaElement);
		//创建字数提示元素和提交按钮
		var extarInfoElement = this._createElement("div",null,this.containerElement);
		dojo.style(extarInfoElement,{
			border:"1px solid red",
			width:this.width
		});
		this.lengthSuggestElement = this._createElement("span",null,extarInfoElement);
		this._setLengthSuggestElement("");
		dojo.style(this.lengthSuggestElement,{
			"float":"left"
		});
		this.submitElement = dojo.doc.createElement('input');
		dojo.attr(this.submitElement,{
			"type":"button",
			"value":this.buttonOk
		});
		dojo.style(this.submitElement,{
			"float":"right"
		});
		extarInfoElement.appendChild(this.submitElement);
		
	},
	_styleElements: function(){
		dojo.style(this.containerElement,{
			width: this.width,
			height: this.height
		});
			console.log(this.titleElement);
		dojo.style(this.titleElement,{
			width:"5px"
		});
		
	},
	_createElement: function(type,value,parentElement){
		var element = dojo.doc.createElement(type);
		element.innerHTML = value || "";
		parentElement.appendChild(element);
		return element;
	},
	_setLengthSuggestElement:function(value){
		var obj = {
			currertContentLength:value.length,
			maxContentLength:this.maxContentLength
		}
		this.lengthSuggestElement.innerHTML = dojo.string.substitute(this.contentLengthSuggestTemplate,obj);
	},
	_bindEvent: function(){
		var that = this;
		dojo.connect(this.textareaElement,"onkeyup",that,"_checkValueLength");
	},
	_checkValueLength: function(e){
		console.log("check");
		var value = this.textareaElement.value;	
		this._setLengthSuggestElement(value);
		if(value && value.length > this.maxContentLength){
			console.log(">>>>>>>>>>>>>");
			dojo.publish("overLength");
		}else{
			this._cancelDealWithOverLength();
		}
	},
	_setLengthSuggestElement:function(value){
		var obj = {
			currertContentLength:value.length,
			maxContentLength:this.maxContentLength
		}
		this.lengthSuggestElement.innerHTML = dojo.string.substitute(this.contentLengthSuggestTemplate,obj);
	},
	_dealWithOverLength: function(data){
		console.log("_dealWithOverLength-->"+this.overLengthErrorInfo);
		if(!dojo.attr(this.submitElement,"disabled")){
			dojo.attr(this.submitElement,{
				disabled:true
			});
		}
	},
	_cancelDealWithOverLength: function(){
		if(dojo.attr(this.submitElement,"disabled")){
				dojo.attr(this.submitElement,{
				disabled:false,
				value:this.buttonOk
			});
		}	
	}
})